import axios from 'axios'

// 创建axios实例
const service = axios.create({
  timeout: 50000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  // Indicator.open({
  //   spinnerType: 'fading-circle'
  // })
  // this.$vux.toast.show({
  //   text: 'Loading'
  // })
  config.headers = {'Content-Type': 'application/x-www-form-urlencoded'}
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
    if (response.data.errno != 0) {
      if (response.data.errno == 100004) {
        let encodeUrl = encodeURI(window.location.href.split('#')[0])
        let url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1406f59bfe205d1d&redirect_uri=' + encodeUrl + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
        document.location = url
      }
    }
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

export default service
